/**
 * 
 */
package com.nari.baseinfo.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.ICity;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.model.IQueryer;
import com.nari.baseinfo.service.ICityService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.baseinfo.service.IQueryerService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class QueryerAction extends JSONActionSupport {
	private static final long serialVersionUID = -3684335880981215388L;
	private IQueryerService iQueryerService;
	private ICityService iCityService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	//查询器信息查询
	private String queryerName;
	private String queryerNo;
	private String districtCityID;
	private String capitalCityID;
	private String provinceCityID;
	private List<ICity> provinceCityList;
	private List<ICity> capitalCityList;
	private List<ICity> districtCityList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IQueryer> queryerPage;
	private Map<String, String> pageCityMap; //所属城市id-cityName映射表
	//查询器信息管理
	private String queryerUID;
	private IQueryer queryer;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private String projectUID;
	
	/**
	 * 查询器分页查询
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-27
	 */
	public String queryQueryerPage() throws Exception {
		//机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if(provinceCityID!=null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		if(capitalCityID!=null && !"".equals(capitalCityID)) {
			this.districtCityList = this.iCityService.queryCityByParentID(capitalCityID);
		} else {
			this.districtCityList = new ArrayList<ICity>();
		}
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		this.queryerPage = this.iQueryerService.queryQueryerPage(queryerName, queryerNo, districtCityID, capitalCityID, provinceCityID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.queryerPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.queryerPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//所属城市映射表
		this.pageCityMap = this.iQueryerService.queryReferCitysByQueryerList(queryerPage.getList());
		return "queryerPage";
	}
	
	/**
	 * 查询器信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-27
	 */
	public String queryQueryerForManage() throws Exception {
		if(queryerUID!=null && !"".equals(queryerUID)) {
			this.queryer = this.iQueryerService.queryEntityById(queryerUID);
			if(queryer != null) {
				//所属城市
				this.provinceCityID = this.queryer.getProvinceCityID();
				this.capitalCityID = this.queryer.getCapitalCityID();
				//所属项目
				this.projectUID = this.queryer.getProjectID();
			}
		}
		//机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if(provinceCityID!=null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		if(capitalCityID!=null && !"".equals(capitalCityID)) {
			this.districtCityList = this.iCityService.queryCityByParentID(capitalCityID);
		} else {
			this.districtCityList = new ArrayList<ICity>();
		}
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属小区列表
		if (projectUID != null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		
		return "queryerManage";
	}
	
	/**
	 * 查询器新增或修改
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-27
	 */
	public String saveOrUpdateQueryer() throws Exception {
		if(this.queryer.getId()!=null && !"".equals(queryer.getId())) {
			this.iQueryerService.updateEntity(queryer, queryer.getId());
		} else {
			this.iQueryerService.saveEntity(queryer);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询器资料删除
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-27
	 */
	public String deleteQueryerByUID() throws Exception {
		this.iQueryerService.deleteEntityById(queryerUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iQueryerService
	 */
	@JSON(serialize=false)
	public IQueryerService getiQueryerService() {
		return iQueryerService;
	}
	
	/**
	 * @param iQueryerService the iQueryerService to set
	 */
	public void setiQueryerService(IQueryerService iQueryerService) {
		this.iQueryerService = iQueryerService;
	}

	/**
	 * @return the iCityService
	 */
	@JSON(serialize=false)
	public ICityService getiCityService() {
		return iCityService;
	}

	/**
	 * @param iCityService the iCityService to set
	 */
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	/**
	 * @return the queryerName
	 */
	public String getQueryerName() {
		return queryerName;
	}

	/**
	 * @param queryerName the queryerName to set
	 */
	public void setQueryerName(String queryerName) {
		this.queryerName = queryerName;
	}

	/**
	 * @return the queryerNo
	 */
	public String getQueryerNo() {
		return queryerNo;
	}

	/**
	 * @param queryerNo the queryerNo to set
	 */
	public void setQueryerNo(String queryerNo) {
		this.queryerNo = queryerNo;
	}

	/**
	 * @return the districtCityID
	 */
	public String getDistrictCityID() {
		return districtCityID;
	}

	/**
	 * @param districtCityID the districtCityID to set
	 */
	public void setDistrictCityID(String districtCityID) {
		this.districtCityID = districtCityID;
	}

	/**
	 * @return the capitalCityID
	 */
	public String getCapitalCityID() {
		return capitalCityID;
	}

	/**
	 * @param capitalCityID the capitalCityID to set
	 */
	public void setCapitalCityID(String capitalCityID) {
		this.capitalCityID = capitalCityID;
	}

	/**
	 * @return the provinceCityID
	 */
	public String getProvinceCityID() {
		return provinceCityID;
	}

	/**
	 * @param provinceCityID the provinceCityID to set
	 */
	public void setProvinceCityID(String provinceCityID) {
		this.provinceCityID = provinceCityID;
	}

	/**
	 * @return the provinceCityList
	 */
	public List<ICity> getProvinceCityList() {
		return provinceCityList;
	}

	/**
	 * @param provinceCityList the provinceCityList to set
	 */
	public void setProvinceCityList(List<ICity> provinceCityList) {
		this.provinceCityList = provinceCityList;
	}

	/**
	 * @return the capitalCityList
	 */
	public List<ICity> getCapitalCityList() {
		return capitalCityList;
	}

	/**
	 * @param capitalCityList the capitalCityList to set
	 */
	public void setCapitalCityList(List<ICity> capitalCityList) {
		this.capitalCityList = capitalCityList;
	}

	/**
	 * @return the districtCityList
	 */
	public List<ICity> getDistrictCityList() {
		return districtCityList;
	}

	/**
	 * @param districtCityList the districtCityList to set
	 */
	public void setDistrictCityList(List<ICity> districtCityList) {
		this.districtCityList = districtCityList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the queryerPage
	 */
	public ResultWithPageCond<IQueryer> getQueryerPage() {
		return queryerPage;
	}

	/**
	 * @param queryerPage the queryerPage to set
	 */
	public void setQueryerPage(ResultWithPageCond<IQueryer> queryerPage) {
		this.queryerPage = queryerPage;
	}

	/**
	 * @return the pageCityMap
	 */
	public Map<String, String> getPageCityMap() {
		return pageCityMap;
	}

	/**
	 * @param pageCityMap the pageCityMap to set
	 */
	public void setPageCityMap(Map<String, String> pageCityMap) {
		this.pageCityMap = pageCityMap;
	}

	/**
	 * @return the queryerUID
	 */
	public String getQueryerUID() {
		return queryerUID;
	}

	/**
	 * @param queryerUID the queryerUID to set
	 */
	public void setQueryerUID(String queryerUID) {
		this.queryerUID = queryerUID;
	}

	/**
	 * @return the queryer
	 */
	public IQueryer getQueryer() {
		return queryer;
	}

	/**
	 * @param queryer the queryer to set
	 */
	public void setQueryer(IQueryer queryer) {
		this.queryer = queryer;
	}

	public List<IProject> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}
	@JSON(serialize = false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	public String getProjectUID() {
		return projectUID;
	}

	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}
	@JSON(serialize = false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}
	
}
